import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';

import { loveRecycActstatusArrs } from '../../love-recyc-manage.pipe';
import { CommonService } from '../../../../services-common/common.service';
import { LoveSendIdolManageService } from '../love-send-idol-manage.service';

@Component({
  selector: 'app-love-send-idol-list',
  templateUrl: './love-send-idol-list.component.html',
  styleUrls: ['./love-send-idol-list.component.scss']
})
export class LoveSendIdolListComponent implements OnInit {

  routeParams: any = {};
  searchOptions: any = [
    { type: 1, labelText: '活动名称', valueKey: 'name', value: '', placeholder: '请输入活动名称/ID' },
    {
      type: 2,
      labelText: '适用成色',
      valueKey: 'colourId',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'id', label: 'name' },
      selectOptions: [],
    },
    {
      type: 2,
      labelText: '活动状态',
      valueKey: 'state',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...loveRecycActstatusArrs],
    },
    { type: 3, labelText: '活动时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true }
  ];
  searchParams: any = {};

  conditionOptionsC: Array<any> = [];

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  constructor(
    private msg: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private commonService: CommonService,
    private services: LoveSendIdolManageService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};
      if (res.activityId) {
        this.searchOptions[0].value = res.activityId;
      }
    })
  }

  ngOnInit(): void {
    this.loadConditionData();
  }

  setSearchOption(array: Array<any>, val: string) {
    let temIndex = this.searchOptions.findIndex((item: any) => item.valueKey === val);
    this.searchOptions[temIndex].selectOptions = array || [];
  }

  // 指定C端成色
  async loadConditionData() {
    this.conditionOptionsC = await this.commonService.getDictList('machine_condition');
    this.setSearchOption(this.conditionOptionsC, 'colourId');
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getLoveActSendIdolList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total} = data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  /**
   * 终止 | 删除
   * @param item 
   * @param state 4：终止 6：删除 
   */
  async overDeleteItem(item: any, state: number) {
    const params = {
      id: item.id,
      state: state
    }
    const {code, data, message}: any = await this.services.editLoveActSendIdolData(params);

    if (code === 0) {
      this.msg.success('操作成功');
      this.tableParams.page = 1;
      this.loadTable();
    }
  }

  /**
   * 设置字典文字（多个）
   * @param arrs 
   * @param arrKey 
   * @param tVal 
   */
  setDictText(arrs: Array<any>, arrKey: any, tVal: any) {
    const valArrs = tVal.split(',');
    let strArrs: Array<any> = [];

    if (arrs.length && valArrs.length) {
      arrs.forEach(item => {
        if (valArrs.includes(String(item[arrKey]))) {
          strArrs.push(item.name);
        }
      })
    }
    return tVal == '0' ? '全部' : strArrs.join('、');
  }
}
